package cuit.lh.remotecontrol;

import cuit.lh.remotecontrol.utlis.JDBCutils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class Home extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton loginButton;
    private JButton registerButton;

    public Home() {
        setTitle("Login or Register");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout()); // 使用GridBagLayout布局管理器

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5); // 设置组件之间的间距

        JLabel usernameLabel = new JLabel("用户名:");
        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(usernameLabel, gbc);

        usernameField = new JTextField(15); // 设置文本字段宽度
        gbc.gridx = 1;
        panel.add(usernameField, gbc);

        JLabel passwordLabel = new JLabel("密码:");
        gbc.gridx = 0;
        gbc.gridy = 1;
        panel.add(passwordLabel, gbc);

        passwordField = new JPasswordField(15); // 设置文本字段宽度
        gbc.gridx = 1;
        panel.add(passwordField, gbc);

        loginButton = new JButton("Login");
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 处理登录逻辑
                String username = usernameField.getText();
                String password = String.valueOf(passwordField.getPassword());
                // 在这里执行登录逻辑，比如验证用户名和密码
                if (username.length() == 0 || password.length() == 0) {
                    JOptionPane.showMessageDialog(Home.this, "用户名或密码为空");
                    return;
                }
                try {
                    int i = login(username, password);
                    if (i == 1)  JOptionPane.showMessageDialog(Home.this, "用户名或密码错误");
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                JOptionPane.showMessageDialog(Home.this, "Login with username: " + username + " and password: " + password);
                RemoteAssistance.start();
                setVisible(false);
            }
        });
        gbc.gridx = 0;
        gbc.gridy = 2;
        panel.add(loginButton, gbc);

        registerButton = new JButton("Register");
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 处理注册逻辑
                String username = usernameField.getText();
                String password = String.valueOf(passwordField.getPassword());
                if (username.length() == 0 || password.length() == 0) {
                    JOptionPane.showMessageDialog(Home.this, "用户名或密码为空");
                    return;
                }
                // 在这里执行注册逻辑，比如将新用户信息写入数据库
                try {
                    int i = register(username, password);
                    if (i == 2) JOptionPane.showMessageDialog(Home.this, "用户名已经存在");
                    return;
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                JOptionPane.showMessageDialog(Home.this, "Register with username: " + username + " and password: " + password);
            }
        });
        gbc.gridx = 1;
        panel.add(registerButton, gbc);

        add(panel);
        setLocationRelativeTo(null); // 将窗口置于屏幕中央
        setVisible(true);
    }
    public static int login(String username, String password) throws SQLException {
        Connection connection = JDBCutils.getConnection();
        String sqlQuery = "SELECT username,password from user where username = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery);
        preparedStatement.setString(1, username);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            String pwd = resultSet.getString("password");
            preparedStatement.close();
            connection.close();
            if (!password.equals(pwd)) return 1;
        } else {
            return 1;
        }
        return 0;
    }
    public static int register(String username, String password) throws SQLException {
        Connection connection = JDBCutils.getConnection();
        String sql = "select count(1) from user where username =' "+username+"'";
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        if (resultSet.next()) {
            statement.close();
            resultSet.close();
            return 2; // 表示用户名已经存在
        }
        String sqlQuery = "INSERT INTO user (username,password) values (?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery);
        preparedStatement.setString(1, username);
        preparedStatement.setString(2, password);
        int i = preparedStatement.executeUpdate();
        preparedStatement.close();
        connection.close();
        return i;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Home();
            }
        });
    }
}
